package br.puc.paa.util;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

public class TimeUtil {

	/** Get CPU time in nanoseconds. "System time" is the time spent running OS code on behalf of your application (such as for I/O).*/
	public static long getCpuTime() {
		ThreadMXBean bean = ManagementFactory.getThreadMXBean();
		return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadCpuTime() : 0L;
	}

	/** Get user time in nanoseconds. "User time" is the time spent running your application's own code.*/
	public static long getUserTime() {
		ThreadMXBean bean = ManagementFactory.getThreadMXBean();
		return bean.isCurrentThreadCpuTimeSupported() ? bean.getCurrentThreadUserTime() : 0L;
	}

	/** Get system time in nanoseconds. "CPU time" is user time plus system time. It's the total time spent using a CPU for your application.*/
	public static long getSystemTime() {
		ThreadMXBean bean = ManagementFactory.getThreadMXBean();
		return bean.isCurrentThreadCpuTimeSupported() ? (bean.getCurrentThreadCpuTime() - bean.getCurrentThreadUserTime()) : 0L;
	}

}
